package com.sctvcloud.qingchuan.ui.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.beans.AccountTabBean;
import com.sctvcloud.qingchuan.ui.fragment.AccountDetailNewsFragment;
import com.sctvcloud.qingchuan.ui.fragment.ColumnBaseFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * 公众号详情--新闻tab适配
 */
public class AccountDetailPagerAdapter extends FragmentStatePagerAdapter {

    public static final int TYPE_NORMAL_NEWS = 1;//普通图文新闻
    public static final int TYPE_IMAGES_NEWS = 2;//组图新闻
    public static final int TYPE_VIDEO_NEWS = 5;//视频新闻

    private List<AccountTabBean> list;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    public AccountDetailPagerAdapter(FragmentManager fm, List<AccountTabBean> list) {
        super(fm);
        this.list = list;
        fragmentWeaks = new SparseArray<>();
    }

    /**
     * 过滤未在设计范围内的栏目
     *
     * @param data 后台来源栏目数据
     * @return 过滤后的栏目数据
     */
    public static List<AccountTabBean> handleColumn(List<AccountTabBean> data) {
        if (ListUtils.isListValued(data)) {
            for (int i = 0; i < data.size(); i++) {
                switch (data.get(i).getType()) {
                    case TYPE_NORMAL_NEWS:
                    case TYPE_IMAGES_NEWS:
                    case TYPE_VIDEO_NEWS:
                        break;
                    default:
                        data.remove(i);
                        break;
                }
            }
        }
        return data;
    }

    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (list.get(position).getType()) {
            case TYPE_NORMAL_NEWS:
            case TYPE_IMAGES_NEWS:
            case TYPE_VIDEO_NEWS:
                fragment = new AccountDetailNewsFragment();
                break;

            default:
                fragment = new AccountDetailNewsFragment();
                break;
        }
        Bundle detail = new Bundle();
        detail.putInt(AccountDetailNewsFragment.NEWS_TYPE, list.get(position).getType());
        detail.putString(AccountDetailNewsFragment.ACCOUNT_ID, list.get(position).getAccountId()  );
        fragment.setArguments(detail);
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getTabName();
    }

    public void setData(List<AccountTabBean> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

    public int getPosCurrentPage(int type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getType() == type) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }


}
